/* **************************************************** */
/*!
   @file	MN88472_sample.c
   @brief	Sample Application for MN88472(Panasonic DVB-T2/T/C Demodulator)
   @author	R.Mori
   @date	2011/7/06
   @param
		(c)	Panasonic
   */
/* **************************************************** */
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include "MN_DMD_driver.h"
#include "MN_DMD_device.h"
#include "MN_DMD_common.h"
#include "MN_DMD_console.h"
#include "MN88473_UserFunction.h"  
#include "MN_I2C.h"

#if (TUNER_TYPE == MXL603)    
#include "MxL603_TunerApi.h"
extern MXL603_TUNER_MODE_CFG_T tunerModeCfg;
extern MXL603_CHAN_TUNE_CFG_T chanTuneCfg;
#endif

#ifdef PC_CONTROL_FE
#define INPUT_ERROR(x)  fprintf( stderr , x )
#define PARAM_STRING(i) DMD_info_value( param->system , i , param->info[i] )
int select_menu( DMD_PARAMETER_t *param );
int select_system( DMD_PARAMETER_t *param );
int iic_test( );
#endif

int main(int argc , char *argv[])
{
	DMD_PARAMETER_t	param;			//Demodulator Parameter
	DMD_u8_t rd;
	
	//Open API
	printf("Loading USB I2C Driver(API Open) ...");
	if( DMD_open(&param) == DMD_E_OK )
		printf("OK\n");
	else
		printf("NG\n");

	//Initialize LSI
	printf("Initializing LSI .. ");
	if( DMD_init(&param) == DMD_E_OK )
		printf("DMD init OK\n");
	else
		printf("DMD init NG - pls. check I2C flow\n");
		
	DMD_device_read_chipid(&rd);
	if(DEMOD_TYPE != rd)
		{
			printf("ERROR !! Wrong match of CHIP[%d] and Defined DMD type[%d]! \n",rd,DEMOD_TYPE);
			return 0;
		}

#ifdef PC_CONTROL_FE 
	select_menu( &param ); 
#else
	if (param.user_scan_action == DMD_E_SCAN_GET_CHANNEL_INFO)
	{
		//for example. don't know what service will be inside. Only set Freq. and Bandwidth
		param.system = DMD_E_DVBT;
		param.freq = 666000000;
		param.bw = DMD_E_BW_8MHZ;
	}
	else if (param.user_scan_action == DMD_E_SCAN_SET_CHANNEL_PARAM)
		{
		//for example; pls. set the value which you stored for one specific service. 
		param.system = DMD_E_DVBT2;
		param.freq = 666000000;
		param.bw = DMD_E_BW_8MHZ;
		param.dvbt2_plp_no = 0;
		param.t2_work_profile_mode = DMD_E_DVBT2_WORK_MODE_LITE;
		param.dvbt_hp_lp_sel = DMD_E_DVBT_HIER_SEL_LP;
	}	
	Scan_one_RF_sample(&param);
#endif    

}

/* **************************************************** */
/*!	User interface  */
/*
Description :
purpose : 1. To find all info. in one RF channel; 
          2. to jump to one service based on stored info. 
input  : //you can also add IF_out_freq, or AGC control, if you want.Normally, not necessary
user_scan_order ->  
  DMD_E_SCAN_GET_CHANNEL_INFO    : for user to get RF changel information, example as below :
		1. param.freq = 666000000;
		2. param.bw = DMD_E_BW_8MHZ;
  DMD_E_SCAN_SET_CHANNEL_PARAM  : for user to jump to one Service(like CCTV5), based on store info. 
		1. param.system = DMD_E_DVBT2;
		2. param.freq = 666000000;
		3. param.bw = DMD_E_BW_8MHZ;
		4. param.dvbt2_plp_no = 1; //DVB-T2 PLP layer number. 
		5. param.t2_work_profile_mode = DMD_E_DVBT2_WORK_MODE_BASE; //DVB-T2 work mode, T2-base or T2-lite. default : BASE
		6. param.dvbt_hp_lp_sel = DMD_E_DVBT_HIER_SEL_LP; //DVB-T work mode, HP or LP. default : LP 
return : ret -> 1 : signal unlocked, or parameter set error; 0. signal locked
Author :  Troy.wangyx, 20120801 
*/
/* **************************************************** */
DMD_ERROR_t Scan_one_RF_sample(DMD_PARAMETER_t *param)
{	 
	DMD_ERROR_t ret = DMD_E_ERROR;
#if (TUNER_TYPE == MXL603)
	MXL603_BW_E tuner_bw;	
	MXL603_SIGNAL_MODE_E signalMode;
#endif

//For DVB-T2
	DMD_u8_t dataPLParray[256];
	DMD_u8_t dataPLPsum = 0;
//For DVB-T 
	DMD_DVBT_HIERARCHY_t hierarchy_exist;
	//DMD_DVBT_HIER_SEL_t hierarchy_selection;

//Set Tuner application mode --- BEGIN ---
#if (TUNER_TYPE == MXL603)
	switch(param->system)
	{
	case DMD_E_DVBT:           
	case DMD_E_DVBT2:        
		signalMode= MXL603_DIG_DVB_T_DTMB;
		switch(param->bw)
		{
			case DMD_E_BW_6MHZ:           
				tuner_bw = MXL603_TERR_BW_6MHz;
			break;
			case DMD_E_BW_7MHZ:          
				tuner_bw = MXL603_TERR_BW_7MHz;
			break;      
			case DMD_E_BW_8MHZ:            
				tuner_bw = MXL603_TERR_BW_8MHz;
			break;
			case DMD_E_BW_5MHZ:
			case DMD_E_BW_1_7MHZ:
			default:  
				printf("--- UNSUPPORT BANDWIDTH!!---\n");
			return DMD_E_ERROR;
		}
		break;      
	case DMD_E_DVBC:   
		signalMode= MXL603_DIG_DVB_C;
		switch(param->bw)
		{         
			default:  
				tuner_bw = MXL603_CABLE_BW_8MHz;
				//For Demod DVB-C, concerns with Symbol rate, not bandwidth		
			break;
		}			
		break;
	case DMD_E_NOT_DEFINED:
	default:
		//default setting
		signalMode= MXL603_DIG_DVB_T_DTMB;
		tuner_bw = MXL603_TERR_BW_8MHz;
		break;
	}
		tunerModeCfg.signalMode = signalMode;
		chanTuneCfg.signalMode = signalMode;
		chanTuneCfg.freqInHz  = param->freq;
		chanTuneCfg.bandWidth = tuner_bw;
#endif
//Set Tuner application mode --- END --- 

   if ( DMD_E_SCAN_GET_CHANNEL_INFO == param->user_scan_action)  
  	{	  
      if ( param->system == DMD_E_DVBC )
      	{
			DMD_set_system(param);
			DMD_scan(param);	 		   
			if (param->info[DMD_E_INFO_LOCK] == DMD_E_LOCKED)
		   	{
		   	    // you find DVB-C frames in this channel 
	           ret = DMD_E_OK;			 
	            printf("--- DVB-C signal found !!---\n");

				;//you may add interface to get programe stream info.(eg. EPG, pid)
			     return ret;
		   	}
      	}
	  else
	  	{
			// Auto recognizing of DVB-T and DVB-T2
			/*----------------------START : Try to lock DVB-T2  ---------------------*/
			//T2 signal, set PLP number,  for SPLP(signal PLP), pls. set PLP number to ZERO. 
			DMD_set_info(param , DMD_E_INFO_DVBT2_SELECTED_PLP , 0);	

			//Tuner and DMD call relative register array, toward different System mode and Bandwidth.
			param->system = DMD_E_DVBT2;
			DMD_set_system(param);

			//Tuner tuning -> DMD scan
			//if you don't change system mode, bandwidth,  you can just use DMD_scan to do Auto search.
			DMD_scan(param);	 

			if (param->info[DMD_E_INFO_LOCK] == DMD_E_LOCKED)
			{			
				DMD_u32_t plp_layer_number = 0;
				ret = DMD_E_OK; 
				
				// you find DVB-T2 frames in this channel 				
				printf("--- DVB-T2 signal found !!---\n");
				/*
				You can use this function to get all PLP and store Data plp info.( one data plp may contain more than one program stream)
				DMD will analyze Data plp and its relative common plp automaticly, so no need to store common plp info.
				How to match RF freq, data plp and program stream ? Store their info. and link each other. 
				*/ 
				//get total PLP number                 
				DMD_get_info(param, DMD_E_INFO_DVBT2_NUM_PLP );		

#ifdef COMMON_DEBUG_ON			   
				printf("--- Total PLP(all types) NUM = %d --- \n", param->info[DMD_E_INFO_DVBT2_NUM_PLP]);
#endif

				if ((param->info[DMD_E_INFO_DVBT2_NUM_PLP] == 0 )||\
				(param->info[DMD_E_INFO_DVBT2_NUM_PLP] > 255 ))//Max 255 PLP
				{
#ifdef COMMON_DEBUG_ON	
					printf("--- DVB-T2, PLP number wrong!!---\n");
#endif
					return DMD_E_ERROR;
				}
				
				//count only Data PLP, to record how many Data PLP in this RF channel
				dataPLPsum = 0;	
				
				//Check if it's data plp, if yes, wait for TS and get PID = programe name; 
				for(plp_layer_number=0;plp_layer_number<param->info[DMD_E_INFO_DVBT2_NUM_PLP];plp_layer_number++)
				{
					if (DMD_get_single_PLP_type(param,plp_layer_number) != DMD_E_OK)
					{
#ifdef COMMON_DEBUG_ON	
					printf("--- DVB-T2, DMD_get_single_PLP[%d]_type wrong !!---\n",plp_layer_number);
#endif
					return DMD_E_ERROR;
					}

					//Find Data PLP
					if( param->info[DMD_E_INFO_DVBT2_DAT_PLP_TYPE] != DMD_E_DVBT2_PLP_TYPE_COM )
					{
					//get data plp's layer number, and recorded in pointer "pPLPIds"			 
					dataPLParray[dataPLPsum] = plp_layer_number; //20131219, for issue37 in update history. 
#ifdef COMMON_DEBUG_ON
					DMD_get_info(param , DMD_E_INFO_DVBT2_DAT_PLP_ID ); 
					printf("--- PLP layer NUM[%d] is DATAPLP\n", plp_layer_number);
#endif            
					dataPLPsum++;

					if(param->info[DMD_E_INFO_DVBT2_SIGNAL_PREAMBLE_COMBI_MODE] == DMD_E_DVBT2_SIGNAL_PREAMBLE_MIXED_BASE_LITE_COEXIST)
					{

#ifdef COMMON_DEBUG_ON	
						printf("--- DVB-T2, BASE+LITE COEXIST SIGNAL!! Pls. choose work mode <BASE> or <LITE>---\n");
#endif
					    /*@@@@@@@@@@-customer can choose BASE or LITE mode later. Need a interface for user to select-@@@@@@@@@@@*/
						;
						/* Retune example :  		
								param->t2_work_profile_mode = DMD_E_DVBT2_WORK_MODE_LITE;
								DMD_set_t2_work_profile( param );
								DMD_device_post_tune(  param );
								DMD_device_scan(  param );
						//[Note]Don't call DMD_device_pre_tune() here = default set T2-Base mode
						
						if (param->info[DMD_E_INFO_LOCK] == DMD_E_LOCKED)
							{
								;//store user selection.
								//param.t2_work_profile_mode = DMD_E_DVBT2_WORK_MODE_BASE or DMD_E_DVBT2_WORK_MODE_LITE
							}
						else
							{
								 ;//pls. ask Demod engineer why cannot lock.
							}
						*/						
					}
					//#######################################################
					//customer may add interface to get programe stream info.(eg. EPG, pid)
					/* Pls. store information for this service, when you want to watch this service, set value then scan.
						param.system ;
						param.freq ;
						param.bw ;
						param.dvbt2_plp_no ;
						param.t2_work_profile_mode ;
					*/
					//########################################################

					}
				}

#ifdef COMMON_DEBUG_ON
				printf("--- Total data PLP NUM = %d --- \n",dataPLPsum);
				for(plp_layer_number=0;plp_layer_number<dataPLPsum;plp_layer_number++)
				{ 		     
				printf("DataPLP No.%d ",plp_layer_number);
				printf("PLP layer No. : %d\n",dataPLParray[plp_layer_number]);
				}
#endif

			 return ret;
		   	}
			/*---------------------END : Try to lock DVB-T2  ---------------------*/
 
		   	/*----------------------START : Try to lock DVB-T  ---------------------*/
			//Tuner and DMD call relative register array, toward different System mode and Bandwidth.
			param->system = DMD_E_DVBT;
			DMD_set_system(param);

			//Tuner tuning -> DMD scan
			DMD_scan(param);	  

			if (param->info[DMD_E_INFO_LOCK] == DMD_E_LOCKED)
			{
				// you find DVB-T frames in this channel 
				ret = DMD_E_OK;			 
				printf("--- DVB-T system found !!---\n");
				
				// Check HP stream exist or not 	 
				DMD_get_info(param, DMD_E_INFO_DVBT_HIERARCHY);			
				hierarchy_exist = param->info[DMD_E_INFO_DVBT_HIERARCHY];
				if ( hierarchy_exist > 0) //if no Hierarchy mode transferred, not allowed selected
				{
#ifdef COMMON_DEBUG_ON	
					printf("--- DVB-T, HP+LP COEXIST SIGNAL!! Pls. choose work mode <HP> or <LP>---\n");
#endif
					/*@@@@@@@@@@-customer can choose HP or LP mode later. Need a interface for user to select-@@@@@@@@@@@*/
					;
					/* Retune example :  		
								param->dvbt_hp_lp_sel = DMD_E_DVBT_HIER_SEL_HP;
								DMD_set_info(param, DMD_E_INFO_DVBT_HIERARCHY_SELECT, param->dvbt_hp_lp_sel);
								DMD_device_post_tune(  param );
								DMD_device_scan(  param );
						if (param->info[DMD_E_INFO_LOCK] == DMD_E_LOCKED)
							{
								;//store user selection.
								//param.t2_work_profile_mode = DMD_E_DVBT_HIER_SEL_HP or DMD_E_DVBT_HIER_SEL_LP
							}
						else
							{
								 ;//pls. ask Demod engineer why cannot lock.
							}
						*/
					//store user selection.
					//param.dvbt_hp_lp_sel = DMD_E_DVBT_HIER_SEL_LP or DMD_E_DVBT_HIER_SEL_HP
				}
				//#######################################################
				//customer may add interface to get programe stream info.(eg. EPG, pid)
					/* Pls. store information for this service, when you want to watch this service, set value then scan.
						param.system ;
						param.freq ;
						param.bw ;
	 					param.dvbt_hp_lp_sel;
					*/
				//#######################################################
			 return ret;
			}	      
			else //no DVB-T nor DVB-T2 signal 
			{
			printf("--- NO VALID SIGNAL FOUND !!---\n");
			ret = DMD_E_ERROR;
			}
	  	}
        /*----------------------END : Try to lock DVB-T/T2  ---------------------*/
 		          
   	}
   else if (DMD_E_SCAN_SET_CHANNEL_PARAM == param->user_scan_action)
   	{
   	
		switch(param->system) 
		{
			case DMD_E_DVBT:     
				DMD_set_info(param, DMD_E_INFO_DVBT_HIERARCHY_SELECT, param->dvbt_hp_lp_sel);
				break;
			case DMD_E_DVBT2: 
				//T2 signal, set PLP number,  for SPLP(signal PLP), pls. set PLP number to ZERO. 
				DMD_set_info(param , DMD_E_INFO_DVBT2_SELECTED_PLP , param->dvbt2_plp_no);	
				break;      

			case DMD_E_DVBC: 
				; //nothing to do..
				break;
			default:          
				ret = DMD_E_ERROR;
		}
		
		DMD_set_system(param);//set T2 work profile inside

		DMD_scan(param);
		if (param->info[DMD_E_INFO_LOCK] != DMD_E_LOCKED)
		{
			printf("--- USER SET CHANNEL INFO, SIGNAL UNLOCKED !!---\n");
		}
		
   	}
	else
	{  
		printf("--- USER ACTION INVALID !!---\n");

		ret = DMD_E_ERROR;
	}

	/*---------------------- status Monitor : Valid after signal locked ---------------------*/
		DMD_get_info(param,DMD_E_INFO_LOCK);
		if(param->info[DMD_E_INFO_LOCK] == DMD_E_LOCKED)
			{
				DMD_u8_t ssi_percent,sqi_percent;
				//SQI : Signal Quality Indicator 
					DMD_get_info(param,DMD_E_INFO_SQI);
					sqi_percent = param->info[DMD_E_INFO_SQI];

				//SSI : Signal Strength Indicator 
				if (param->system == DMD_E_DVBT2)
					{
						ssi_percent = Calc_T2_SSI(param);
					}
				else if(param->system == DMD_E_DVBT)
					{
						sqi_percent = Calc_T_SSI(param);
					}
				else //DVB-C
					{
					// use function DMD_AGC();
					}

				//other info.
				//DMD_get_info(param,DMD_E_INFO_xxx); //refer to structure DMD_INFO_t 
				//rd = param->info[DMD_E_INFO_xxx];				
			}
	
	return ret;
}

//MPLP Information sample
DMD_ERROR_t	dvbt2_mplp_information_sample( DMD_PARAMETER_t*	param )
{
	DMD_u32_t i;
	DMD_u8_t	pPLPIds[256];
	DMD_u8_t	pNumPLPs;


	DMD_get_dataPLPs(  pPLPIds, &pNumPLPs , param );
	for(i=0;i<pNumPLPs;i++)
	{
		 printf("No.%d ",i);
		 printf("ID:%04x ",pPLPIds[i]);
		 printf("\n");
	}
	return DMD_E_OK;
}


/*
The functions below are using PC communication protocal, 
Normally, the code below is just for refercence of system flow: Set system-> tuning -> scan ->  lock signal 

After you get the source code, if you want to compile and connect PC with FE ,
you can open the define as below. 
After compile, it will generate CMD box, try to communicate with FE. Don't forget to run with folder "i2c_fx2i2c_for_PC"
*/
#ifdef PC_CONTROL_FE 

/*! Main Function */
#define BUF_MAX	256
int tune( DMD_PARAMETER_t *param )
{
	char	buf[BUF_MAX];

	printf(" Frequency [MHz]:" );
	fgets( buf , BUF_MAX , stdin );

	param->freq = atoi( buf );
	param->funit = DMD_E_MHZ;

	DMD_tune( param );

	return 0;
}

int iic_test( ){
	char	buf[BUF_MAX];
	DMD_u8_t	slv,adr,dat;
	printf("IIC Test [r..read , w..write , q..quit]>");
	fgets( buf , BUF_MAX , stdin );


	switch( buf[0] ){
		case 'r':
			printf( "slave>" );
			fgets( buf , BUF_MAX , stdin );
			slv = (DMD_u8_t)strtol( buf , NULL , 16 );
			printf( "adr  >" );
			fgets( buf , BUF_MAX , stdin );
			adr = (DMD_u8_t)strtol( buf , NULL , 16 );
			DMD_I2C_Read( slv ,adr , &dat) ;
			printf("read >%x\n",dat);
			break;

		case 'w':
			printf( "slave>" );
			fgets( buf , BUF_MAX , stdin );
			slv = (DMD_u8_t)strtol( buf , NULL , 16 );
			printf( "adr  >" );
			fgets( buf , BUF_MAX , stdin );
			adr = (DMD_u8_t)strtol( buf , NULL , 16 );
			printf( "data >" );
			fgets( buf , BUF_MAX , stdin );
			dat = (DMD_u8_t)strtol( buf , NULL , 16 );
			DMD_I2C_Write( slv ,adr , dat) ;
			break;
		case 'q':			return 0;
	}

	return 1;
}

//Set PLP No sample
DMD_ERROR_t dvbt2_set_plp_sample(  DMD_PARAMETER_t *param )
{
	char	buf[BUF_MAX];
	printf("Select PLP No.(default=0)>" );
	fgets( buf , BUF_MAX , stdin );
	//select PLP No
	return DMD_set_info(  param , DMD_E_INFO_DVBT2_SELECTED_PLP , atoi( buf ) );
}

//Tranmission mode preset sample
DMD_ERROR_t dvbt_mode_preset_sample( DMD_PARAMETER_t *param )
{
	char	buf[BUF_MAX];
	printf("Use Mode Preset?(y/n)" );
	fgets( buf , BUF_MAX , stdin );
	if( buf[0] == 'y' )
	{
		printf("Select Mode(0..2k, 1..8k)>");
		fgets( buf , BUF_MAX , stdin );
		switch( buf[0] )
		{ 
			//set Mode preset
		case '0':
			DMD_set_info( param , DMD_E_INFO_DVBT_MODE , DMD_E_DVBT_MODE_2K );
			break;
		case '1':
			DMD_set_info(  param , DMD_E_INFO_DVBT_MODE , DMD_E_DVBT_MODE_8K );
			break;
		default:
			//To run mode&gi auto search , set both Mode & GI to NOT DEFINED .  
			DMD_set_info(  param , DMD_E_INFO_DVBT_MODE , DMD_E_DVBT_MODE_NOT_DEFINED );
			DMD_set_info(  param , DMD_E_INFO_DVBT_GI   , DMD_E_DVBT_GI_NOT_DEFINED );
			printf("Mode Error\n");
			return DMD_E_ERROR;
		}
		printf("Select GI(0..1/32, 1..1/16 , 2..1/8 , 3..1/4)>");
		fgets( buf , BUF_MAX , stdin );
		switch( buf[0] )
		{
			//set GI preset
		case '0':
			DMD_set_info( param , DMD_E_INFO_DVBT_GI , DMD_E_DVBT_GI_1_32 );
			break;
		case '1':
			DMD_set_info( param , DMD_E_INFO_DVBT_GI , DMD_E_DVBT_GI_1_16 );
			break;
		case '2':
			DMD_set_info( param , DMD_E_INFO_DVBT_GI , DMD_E_DVBT_GI_1_8 );
			break;
		case '3':
			DMD_set_info( param , DMD_E_INFO_DVBT_GI , DMD_E_DVBT_GI_1_4 );
			break;
		default:
			DMD_set_info( param , DMD_E_INFO_DVBT_MODE , DMD_E_DVBT_MODE_NOT_DEFINED );
			DMD_set_info( param , DMD_E_INFO_DVBT_GI   , DMD_E_DVBT_GI_NOT_DEFINED );
			printf("GI Error\n");
			return DMD_E_ERROR;
		}

	}
	else
	{
		DMD_set_info(  param , DMD_E_INFO_DVBT_MODE , DMD_E_DVBT_MODE_NOT_DEFINED );
		DMD_set_info(  param , DMD_E_INFO_DVBT_GI   , DMD_E_DVBT_GI_NOT_DEFINED );
	}

	return DMD_E_OK;

}
//Menu
int select_menu( DMD_PARAMETER_t *param )
{
	char	buf[BUF_MAX];
	DMD_u32_t	i,st,ed,s;
	DMD_u32_t	exit_flag=0;
#if (TUNER_TYPE == MXL603)
    MXL603_BW_E tuner_bw;	
    MXL603_SIGNAL_MODE_E signalMode;
#endif

	while( exit_flag == 0 )
	{
		printf( "<MN8847x sample Menu>\n");
		printf( "0.. select broadcast system( DVBT2/T/C )\n");
		printf( "1.. tune ( one freqency )\n");
 		printf( "2.. scan ( freqency range )\n");
		printf( "3.. show the demodulator status( detailed modulation info.)\n");		
		printf( "m.. status monitor(once 1 sec.)\n");
		printf( "4.. [ DVB-T2 ]  PLPs info- select data PLP(default PLP 0) \n");
		printf( "5.. [ DVB-T  ]  Select HP or LP \n");
		printf( "6.. [ DVB-T2 ]  Profile select: T2-base or T2-Lite\n");
		printf( "c.. check Carrier Bias & XTAL Bias \n");
		printf( "z.. Demod iic test\n");
		printf( "d.. debug demod sw-reset \n");
		printf( "e.. debug read all demod registers \n");
		printf( "q.. quit application\n");
		printf( "select no.>");
		fgets( buf , BUF_MAX , stdin );

		switch( buf[0] ){
			case '0':	
				DMD_con_select_system(param );	
				//Set Tuner application mode --- BEGIN ---
				 #if (TUNER_TYPE == MXL603)
					switch(param->system)
					{
					case DMD_E_DVBT:           
					case DMD_E_DVBT2:        
							signalMode= MXL603_DIG_DVB_T_DTMB;
						 	switch(param->bw)
						    {
							case DMD_E_BW_6MHZ:           
									tuner_bw = MXL603_TERR_BW_6MHz;
							        break;
							case DMD_E_BW_7MHZ:          
									tuner_bw = MXL603_TERR_BW_7MHz;
							        break;      
							case DMD_E_BW_8MHZ:            
									tuner_bw = MXL603_TERR_BW_8MHz;
							        break;
							case DMD_E_BW_5MHZ:
							case DMD_E_BW_1_7MHZ:
							default:  
								       printf("--- UNSUPPORT BANDWIDTH!!---\n");
									  return DMD_E_ERROR;
						    }
					    break;      
					case DMD_E_DVBC:           
							signalMode= MXL603_DIG_DVB_C;
							switch(param->bw)
						    {         
							default:  
								tuner_bw = MXL603_CABLE_BW_8MHz;
										//For Demod DVB-C, concerns with Symbol rate, not bandwidth		
								break;
						    }
							
					    break;  	
					default:  
						       printf("--- UNSUPPORT SYSTEM!!---\n");
							  return DMD_E_ERROR;
					}
						tunerModeCfg.signalMode = signalMode;
						chanTuneCfg.signalMode = signalMode;
						chanTuneCfg.bandWidth = tuner_bw;
					 #endif
				//Set Tuner application mode --- END --- 
				
				DMD_set_system( param );
				break;
			case '1':	
				if( param->system == DMD_E_DVBT )
					{
					//DVBT mode preset
					//dvbt_mode_preset_sample( param);// Use default setting, cause customer didn't care
					DMD_set_info(  param , DMD_E_INFO_DVBT_MODE , DMD_E_DVBT_MODE_NOT_DEFINED );
					DMD_set_info(  param , DMD_E_INFO_DVBT_GI   , DMD_E_DVBT_GI_NOT_DEFINED );	
					}
				else if( param->system == DMD_E_DVBT2 )
					{
					//DVBT2 plp no.
					//dvbt2_set_plp_sample( param ); // Use default setting, cause customer didn't care
					DMD_set_info(  param , DMD_E_INFO_DVBT2_SELECTED_PLP , 0);
					}
			
				DMD_con_input_freq(param );
				param->freq *= 1000; //unit : Hz 
				//Set Tuner application mode --- BEGIN ---
				 #if (TUNER_TYPE == MXL603)
						chanTuneCfg.freqInHz  = param->freq;
				 #else
				 #endif
				//Set Tuner application mode --- END --- 

				//DMD_tune(param );
				 DMD_scan(param );
				break;

			case '2':
				printf( "start freq[Khz]>");
				fgets( buf , BUF_MAX , stdin );
				st = atoi( buf );	
				printf( "end   freq[Khz]>");
				fgets( buf , BUF_MAX , stdin );
				ed = atoi( buf );	
				printf( "step  freq[Khz] -- Same as the bandwidth >");
				fgets( buf , BUF_MAX , stdin );
				s = atoi( buf );	
				for( i=st;i<=ed;i+=s )
				{
					param->freq = i;
					param->funit = DMD_E_KHZ;
					printf("Search %4d[Khz] BEGIN: ",i );
					param->freq *= 1000; //unit : Hz 
					//Set Tuner application mode --- BEGIN ---
					 #if (TUNER_TYPE == MXL603)
							chanTuneCfg.freqInHz  = param->freq;
					 #endif
					//Set Tuner application mode --- END --- 

					DMD_scan( param );
					printf("Search %4d[Khz] END: ",i );
					if (param->info[DMD_E_INFO_LOCK] == DMD_E_LOCKED)
					{
						printf("----------------[ Success ] demod Locked-------------------------");
					}
					else
					{
						printf("-----------------[ Fail ]demod Unlocked-----------------------");
					}
					printf("\n");
					DMD_wait(2000);
				}

				break;

			case '3':	
				DMD_get_info( param , DMD_E_INFO_ALL );	
				DMD_con_show_status(param);
				break;

			case '4':
				DMD_get_info(param, DMD_E_INFO_LOCK);
				if ((param->info[DMD_E_INFO_LOCK] == DMD_E_LOCKED)&&(param->system != DMD_E_DVBT2))
					{
					printf( "Signal locked. the valid data PLPs you have are : \n ");
					dvbt2_mplp_information_sample( param );				
					printf( "MPLP : pls. set the PLP ID[0~255]> ");
					fgets( buf , BUF_MAX , stdin );
					i = atoi( buf );	
					//select data PLP ID
					DMD_set_info( param , DMD_E_INFO_DVBT2_SELECTED_PLP , i );		 
					DMD_wait( 300); 
					//wait demod to analyze this PLP,
					//if no such PLP(empty PLP), PER will be > 0. then you cannot see video output
					}
				else
					{
					printf( " INVALID !! signal unlocked or DTV system not DVBT2 \n");            
					}
				break;

            case '5':
					printf( "[ DVB-T  ]  Pls.input (0 : LP(default), 1: HP ) :\n");
					fgets( buf , BUF_MAX , stdin );
					i = atoi( buf );	
					DMD_SET_DVBT_HP_LP_MODE(param,i);
				break;
				
			case '6':
					printf( "[ DVB-T2 work profile]  Pls.input (0 : T2_BASE, 1: t2_LITE ):\n");
					fgets( buf , BUF_MAX , stdin );
					i = atoi( buf );						
					param->t2_profile_type = i;
					DMD_set_t2_work_profile(param);
				break;
				
			case 'c':
				DMD_get_info(param, DMD_E_INFO_LOCK);
				if (param->info[DMD_E_INFO_LOCK] == DMD_E_LOCKED)
					{
						DMD_Carrier_Frequency_bias(param);
						DMD_XTAL_Frequency_bias(param);
					}
		        else
		        	{
					printf( " INVALID !! signal unlocked. \n");  
		        	}
				break;
            
			case 'm':
				printf("Press key to  exit\n");
				while( !_kbhit() )
				{
					DMD_get_info( param,DMD_E_INFO_ALL);
					printf("SQI: %3d " , param->info[DMD_E_INFO_DVBT_SQI]);
					DMD_con_show_monitor(param);	
					//DMD_Carrier_Frequency_bias(param);
					DMD_wait(1000);
				}
				break;
			case 'z':	while(iic_test( ));	break;
			case 'd':	
				DMD_device_post_tune( param );
				printf("demod sw-reset done ! \n");
				break;
			case 'e':	
				DMD_read_registers();
				break;	
#ifdef DMD_I2C_DEBUG
			case 'x':
				dmd_i2c_debug_flag ^= 1;
				break;
#endif
			case 'q':	exit_flag=1; break;
			default:	
				INPUT_ERROR("Not Exists\n");
		}
	}
	return 1;
}
#endif //#ifdef PC_CONTROL_FE


